/**
 * \file
 *           tlmRmtCfgPara.cpp
 * \brief
 *           Implements tlmRmtCfgPara class for Telematics services
 *           this is the base class of parameters of a service.
 *
 * \par Author (last changes):
 *          - Zeng Jie
 *          - BUIC, Continental Automotive Holdings Co., Ltd.
 * \par Project Leader:
 *          -
 * \par Responsible Developer:
 *          - Zeng Jie
 *          - BUIC, Continental Automotive Holdings Co., Ltd.
 * \par Responsible Architect:
 *          - Zhang Fa
 *          - BUIC, Continental Automotive Holdings Co., Ltd.
 *
 * \par Project:
 *      DPCA2
 *
 * \par SW-Component:
 *      Telematics
 *
 * \par SW-Package:
 *      Telematics
 *
 * \par SW-Module:
 *      Telematics Application
 *
* \par SW-Architecture Specification:
 *      DPCA2_TELEMATICS_SW_DD_07a_ready.doc
 *
* \par SW-Design Specification:
 *      DPCA2_TLM-RMTCFG_SW_DD_01d_released.doc
 *
 * \par SW-Test Case Specification:
 *
 * \par CM-Info:
 *
 * \note
 *      This is a note.
 *
 * \bug
 *      - there are no known bugs
 *
 * \par Module-History:
 *
 * \verbatim
 * Date                Author					Reason
 * 10 Dec 2013        Zeng Jie              Initial Creation
 * The latest entry at the head of the history list.
 * \endverbatim
 *
 * \par Copyright Notice:
 * Copyright (C) 2012 Continental Automotive Holdings Co., Ltd.
 * Alle Rechte vorbehalten. All Rights Reserved.\n
 * The reproduction, transmission or use of this document or its contents is
 * not permitted without express written authority.\n
 * Offenders will be liable for damages. All rights, including rights created
 * by patent grant or registration of a utility model or design, are reserved.
 *
 */

#include "stdafx.h"
#include "tlmRmtCfgPara.h"
#include "msgbuf.h"
//## operation GetItem(uint8_t)
#include "tlmRmtCfgElementPara.h"
#include "teleLogTrace.h"


/**
* \brief tlmRmtCfgPara: this is constructor of tlmRmtCfgPara class
*
* \note
*
* \param[in]
* - eleCount: the count of element for this parameters
*
* \return
* - none
*/
tlmRmtCfgPara::tlmRmtCfgPara(uint32_t eleCount) : mEleParaCount(eleCount), mEleParaList(NIL), mIsRequested(FALSE), mMsgBuf(NIL), mSrvId() {
   
    //#[ operation tlmRmtCfgPara(uint32_t)
    mEleParaList = new tlmRmtCfgElementPara*[eleCount];     
    //mEleParaCount = eleCount;
    
    //#]
}

/**
* \brief tlmRmtCfgPara: this is default constructor of tlmRmtCfgPara class
*
* \note
*
* \param[in]
* - none
*
* \return
* - none
*/
tlmRmtCfgPara::tlmRmtCfgPara() : mEleParaCount(0), mEleParaList(NIL), mIsRequested(FALSE), mMsgBuf(NIL), mSrvId() {
}

/**
* \brief ~tlmRmtCfgPara: this is destructor of tlmRmtCfgPara class
*                        it shall delet parameter element list
*
* \note
*
* \param[in]
* - none
*
* \return
* - none
*/
tlmRmtCfgPara::~tlmRmtCfgPara() {
    if(mEleParaList!=NIL)
    {
    	delete [] mEleParaList;
    }
}

/**
* \brief ClearRequested: clear request flag
*
* \note
*
* \param[in]
* - none
*
* \return
* - none
*
*/
void tlmRmtCfgPara::ClearRequested() {
    
    //#[ operation ClearRequested()
    mIsRequested=FALSE;
    //#]
}

/**
* \brief Fill : fill the parameter buffer. 
*               This function run all the parameter element in the list, and fill it's value in the message
*
* \note
*
* \param[in]
* - none
*
* \return
* - none
*
*/
void tlmRmtCfgPara::Fill() {
    
    //#[ operation Fill()
    if(mIsRequested)
    {
        mMsgBuf->setValue(SEPERATE_LEVEL_2);  

	  mMsgBuf->setValue(mSrvId);
        
        for(uint32_t i=0; i<mEleParaCount; i++)
        {
        	mEleParaList[i]->Fill(mMsgBuf);
        }
    }
    //#]
}

/**
* \brief GetEleParaList: get the parameter element list
*
* \note
*
* \param[in]
* - none
*
* \return
* - tlmRmtCfgElementPara**: pointer the element point list
*
*/
tlmRmtCfgElementPara** tlmRmtCfgPara::GetEleParaList() const{
    //#[ operation GetEleParaList()
    return mEleParaList;
}

/**
* \brief GetEleParaCount: get count of the parameter element 
*
* \note
*
* \param[in]
* - none
*
* \return
* - uint32_t: count of the para element
*
*/
uint32_t tlmRmtCfgPara::GetEleParaCount() const{
    //#[ operation GetEleParaList()
    return mEleParaCount;
}

/**
* \brief IsRequested : check if the element is requested to send to offboard server 
*
* \note
*
* \param[in]
* - none
*
* \return
* - BOOL : TRUE - the element is requested
*          FALSE - the element is not requested
*
*/
BOOL tlmRmtCfgPara::IsRequested() const{
    return mIsRequested;
}

/**
* \brief : Save - save the parameter into NVM
*                 This is the base virtual function, as differemnt paramenter of service
*                 has different element, the derived class shall implement it.
*
* \note
*
* \param[in]
* - none
*
* \return
* - uint8_t : the save status.
*
*/
uint8_t tlmRmtCfgPara::Save() {
    
    //#[ operation Save()
    //#]
	return 0;
}
/**
* \brief SetMsgBuf : set the message buf object pointer
*
* \note
*
* \param[in]
* - pMsgBuf: pointer to message buf object that shall be set
*
* \return
* - none
*
*/
void tlmRmtCfgPara::SetMsgBuf(msgbuf* pMsgBuf) {
    
    //#[ operation SetMsgBuf(msgbuf)
    mMsgBuf = pMsgBuf;
    //#]
}

/**
* \brief SetRequested: set mIsRequested flag as true
*
* \note
*
* \param[in]
* - none
*
* \return
* - none
*
*/
void tlmRmtCfgPara::SetRequested() {
    
    //#[ operation SetRequested()
    //LOG_PRINT(DPCA_TELE_MAIN_INFO,"[TLM_MSGSRV]:<tlmRmtCfgSrvApp::%s>,[%d] \n", __FUNCTION__,TRUE);
    mIsRequested=TRUE;
    //#]
}

/**
* \brief SetRequested: set mIsRequested as flag value
*
* \note
*
* \param[in]
* - flag: the value shall be set to mIsRequested
*
* \return
* - none
*
*/
void tlmRmtCfgPara::SetRequested(BOOL flag) {
    //LOG_PRINT(DPCA_TELE_MAIN_INFO,"[TLM_MSGSRV]:<tlmRmtCfgSrvApp::%s>,[%d] \n", __FUNCTION__,flag);
    mIsRequested=flag;
}

/**
* \brief Update:update the parameter by another one
*
* \note
*
* \param[in]
* - none
*
* \return
* - none
*
*/
void tlmRmtCfgPara::Update(tlmRmtCfgPara* pPara) const{ /*DG CPP2ISQP-ISQPcpp Rule 8.7.1-SCC_GRANT_0006*/
    tlmRmtCfgElementPara ** pList=NIL;
    uint32_t tmpCnt=0;
    if(NIL == pPara)
    {
	    //LOG_PRINT(DPCA_TELE_MAIN_ERROR,"[TLM_MSGSRV]:<tlmRmtCfgPara::%s>, pPara is NIL\n", __FUNCTION__);
    }
    else
    {
        pList = pPara->GetEleParaList();
        tmpCnt = pPara->GetEleParaCount();// get the element count of the source parameter

        if(tmpCnt != mEleParaCount)
        {//both parameter has different count, they are not same class.
	        LOG_PRINT(DPCA_TELE_MAIN_ERROR,"[TLM_MSGSRV]<%s>, different parameter\n", __FUNCTION__);
        }
        else
        {
            for(uint32_t i=0; i<mEleParaCount; i++)   
            {
                mEleParaList[i]->Update(pList[i]);
            }
        }

    }
}

/**
* \brief SaveParameters: save parameters into persistency.
*
* \note
*
* \param[in]
* - id: persistency key id
* - buf: pointer to buffer that shall to save into persistency.
* - size: size that shall be saved.
*
* \return 
* - uint8_t: save status:
*            1 - save failed
*            0 - save successfully.
*
*/
uint8_t tlmRmtCfgPara::SaveParameters(uint32_t id, uint8_t* buf, uint32_t size) {

    DWORD status = 0;
    uint8_t retVal = 0;

    LOG_PRINT(DPCA_TELE_MAIN_INFO,"[TLM_MSGSRV]<%s> [id:0x%x],[size:%d]\n", __FUNCTION__, id, size);

    status = ssw_ergUserWriteData(ERG_USER_SYSTEM, id,static_cast<UINT8*> (buf), size);

    if(status != ER_ERG_SUCCESS)
    {
        LOG_PRINT(DPCA_TELE_MAIN_INFO,"[TLM_MSGSRV]: Save parameters[id:0x%x] Error=%lx \n",id, status);
        retVal = 1;
    }
    
    return retVal;
}
